package com.mhy.utils.toast;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.StringRes;

import com.mhy.utils.R;

import java.nio.charset.Charset;

/**
 * 防止重复弹出同样的toast
 * Short< 10汉字 < Long   2秒  3.5秒
 * 居中显示的 toast
 *
 * @author zengliang
 */
public class ToastUtil {

    private static Toast mToast;

    private static void toast(Context context, CharSequence msg, int gravity, int xOffset, int yOffset) {
        Context ctx = context.getApplicationContext();
        if (null == ctx) {
            ctx = context;
        }
        if (null == ctx) {
            return;
        }
        //自定义布局
        View view = LayoutInflater.from(context).inflate(R.layout.item_toast, null);
        TextView mTextView = (TextView) view.findViewById(R.id.toast_message);
        mTextView.setText(msg);
        hide();
        mToast = null;
        mToast = new Toast(context);
        //设置toast 位置
        mToast.setGravity(gravity, xOffset, yOffset);
        mToast.setView(view);
        if (msg.toString().getBytes(Charset.forName("GBK")).length < 20) {
            mToast.setDuration(Toast.LENGTH_SHORT);
            show();
        } else {
            mToast.setDuration(Toast.LENGTH_LONG);
            show();
        }
    }

    //默认的xOffset
    private static int sDefaultXOffset;
    //默认的yOffset
    private static int sDefaultYOffset;

    /**
     * @param msg 弹出Toast的内容
     */
    public static void showToast(Context context, CharSequence msg) {
        if (null == context) {
            return;
        }
        toast(context, msg, Gravity.CENTER, 0, 0);
    }

    /**
     * @param resId 弹出Toast的内容的资源ID
     */
    public static void showToast(Context context, @StringRes int resId) {
        if (null == context) {
            return;
        }
        showToast(context, context.getResources().getString(resId));
    }

    public static void showTopToast(Context context, CharSequence msg) {
        if (null == context) {
            return;
        }
        toast(context, msg, Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    public static void showTopToast(Context context, @StringRes int resId) {
        if (null == context) {
            return;
        }
        showTopToast(context, context.getResources().getString(resId));
    }

    private static Toast toast;

    /**
     * @param msg 弹出Toast的内容
     */
    public static void showBottomToast(Context context, CharSequence msg) {
        if (null == context) {
            return;
        }
        if (toast == null) {
            //创建Toast单例，并保存默认的xOffset和yOffset
            mToast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
            sDefaultXOffset = toast.getXOffset();
            sDefaultYOffset = toast.getYOffset();
        }
        toast(context, msg, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, sDefaultXOffset, sDefaultYOffset);
    }

    /**
     * @param resId 弹出Toast的内容的资源ID
     */
    public static void showBottomToast(Context context, @StringRes int resId) {
        if (null == context) {
            return;
        }
        showBottomToast(context, context.getResources().getString(resId));
    }

    private static void show() {
        mToast.show();
    }

    /**
     * 隐藏toast
     */
    private static void hide() {
        if (mToast != null) {
            mToast.cancel();
        }
    }


}
